import BasePool from "../../db/BasePool";

export default class loginModel extends BasePool {
	/**
	 * 登录
	 **/
	// 登录
	async signIn(params){
		let { loginuser } = params;
		let sql = `select * from tbl_user where status!=0 and loginuser=?`
		// let sql = `SELECT u.* FROM tbl_user u, tbl_role r WHERE u.roleid = r.id AND u. STATUS != 0 AND r. STATUS = 1 AND loginUser =? AND loginPwd =?`;
		return await this.queryOne(sql,loginuser);
	}
	// 注册
	async register(obj){
		let sql="insert into tbl_user set ? ";
		return await this.query(sql,obj);
	}
	// 查询账号是否存在
	async getLoginUser(loginUser){
		let sql="select * from tbl_user where status!=0 and loginuser=?";
		return await this.query(sql,loginUser);
	}
	// 更新用户登陆时间
	async updateLoginTime(id){
		let sql="update tbl_user set loginTime=now() where id=?";
		return await this.query(sql,id);
	}
	// 获取用户权限
	async getUserMenu(params){
		let sql = `SELECT m.* FROM tbl_user u, tbl_role r, tbl_role_menu rm, tbl_menu m WHERE u.roleid = r.id AND r.id = rm.roleid AND rm.menuid = m.id AND u.id = ? AND u. STATUS = 1 AND r. STATUS = 1 AND rm. STATUS = 1 AND m. STATUS = 1 AND m.menuType = 'menu' ORDER BY m.sort`;
		return await this.query(sql,params.id);
	}
	// 查询meta
	async getMeta(menuid){
			let sql = `SELECT * FROM tbl_menu_meta WHERE status=1 AND menuid=?`
			return await this.query(sql,menuid);
	}
	// 查询用户按钮权限列表
	async getAuthBtnList(roleid){
		let sql = `SELECT m.btnPower FROM tbl_role_menu rm, tbl_menu m WHERE rm.menuid = m.id AND rm.status = 1 AND m.status = 1 AND rm.roleid = ? AND m.menuType='btn'`
		return await this.query(sql,roleid);
	}
	// 根据token查询用户信息
	async getUserInfoById(userId){
		let sql = `select * from tbl_user where status=1 and id=?`
		// let sql = `SELECT u.* FROM tbl_user u, tbl_role r WHERE u.roleid = r.id AND u. STATUS = 1 AND r. STATUS = 1 AND u.id =?`
		return await this.queryOne(sql,userId);
	}

}
